package lerry.load.loader;
import flash.display.Bitmap;
import flash.display.DisplayObject;
import flash.display.Loader;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import flash.utils.Object;
import lerry.load.loader.events.QueueEvent;

/**
 * ...
 * @author berry
 */
[Event(name="child",type="lxq.berry.loadtool.queue.events.QueueEvent")]
[Event(name="all",type="lxq.berry.loadtool.queue.events.QueueEvent")]
[Event(name="progress",type="lxq.berry.loadtool.queue.events.QueueEvent")]
[Event(name="error",type="lxq.berry.loadtool.queue.events.QueueEvent")]

public class QueueLoader extends EventDispatcher {
	private var loader:Loader;//加载器
	private var current:Array;//存储对象url和key
	private var startIndex:Int;//加载开始的索引
	private var objs:Object;//存储已加载的对象，包含对象实例和key
	private var keys:String;//根据此值获取对象
	private var skipError:Bool;//是否跳过加载错误
	private var autoDispose:Bool;//是否自动销魂
	private var progress:Float;//加载的进度
	private var len:Int;//长度
	private var loaded:Array<Bitmap>;//所有已加载的对象的数组
	
	public function new(skipError:Boolean = true, autoDispose:Boolean = true) {
		this.loader = new Loader();
		this.startIndex = 0;
		this.progress = 0;
		
		this.objs = {};
		this.current = [];
		this.skipError = skipError;
		this.autoDispose = autoDispose;
		this.results = new Array<Bitmap>();
	}
	//此方法用于将图片地址及key压入数组
	public function push(url:String, key:String = ""):void {
		var obj:Object = {};
		obj.url = url;
		if (key != "") {
			obj.key = key;
		}
		current.push(obj);
	}
	//调用此方法开始加载
	public function start():void {
		len = current.length;
		load();
	}
	//通过key获取已加载的图片
	public function getChildByKey(key:String):DisplayObject {
		for (var k:String in objs) {
			if (k == key) {
				return objs[k]
			}
		}
		;
		return null;
	}
	
	function load():void {
		loader.load(new URLRequest(current[startIndex].url));
		loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComoleted);
		loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
	}
	
	function onError(e:IOErrorEvent):void {
		dispatchEvent(new QueueEvent(QueueEvent.LOAD_ERROR, e));
		if (skipError) {
			startIndex++;
			if (startIndex > current.length - 1) {
				dispatchEvent(new QueueEvent(QueueEvent.ALL_COMPLETE, "length of all loaded images:" + results.length));
			} else {
				load();
			}
		} else {
			dispatchEvent(new QueueEvent(QueueEvent.ALL_COMPLETE, "length of all loaded images:" + results.length));
		}
	}
	
	function onLoadComoleted(e:Event):void {
		loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadComoleted);
		if (current[startIndex].key) {
			objs[current[startIndex].key] = loader.content;
		}
		loaded.push(loader.content);
		dispatchEvent(new QueueEvent(QueueEvent.CHILD_COMPLETE, current[startIndex].url));
		startIndex++;
		progress = startIndex / len * 100;
		dispatchEvent(new QueueEvent(QueueEvent.PROGRESS, progress));
		if (startIndex > current.length - 1) {
			dispatchEvent(new QueueEvent(QueueEvent.ALL_COMPLETE, "length of all loaded images:" + results.length));
			if (autoDispose) {
				dispose();
			}
		} else {
			load();
		}
	}
	//销毁
	public function dispose():void {
		current.length = 0;
		loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError);
	}
	//存储所有加载结果的数组
	public function results():Array<Bitmap> {
		return loaded;
	}
	
}